﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PepsiCo.Business;
using System.Configuration;
using System.Text;

namespace PepsiCo.UI
{
    public partial class Aquarius : BaseMaster
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurUser == null) return;
                LoadNavigation();
                ltrLastVisit.Text = Application["viewpage"].ToString();
                ltrOnline.Text = Application["visit"].ToString();
            }
        }

        private void LoadNavigation()
        {
            StringBuilder nav = new StringBuilder(
                "<ul id='mainNav' class='navigation'>" +
                "   <li class='active'><a href = '" + ResolveUrl(DefaultURL) + "'><span class='isw-grid'></span><span class='text'>Dashboard</span></a></li>"
                );
            var func = DB.pepsi_Functions
                        .Where(p => p.pepsi_Function_Roles.Any(fr =>
                                    fr.pepsi_Role.pepsi_Role_Groups.Any(rg =>
                                       rg.pepsi_Group.pepsi_User_Groups.Any(ug =>
                                          ug.UserID == CurUser.UserID))
                                    ||
                                    fr.pepsi_Role.pepsi_User_Roles.Any(ur =>
                                        ur.UserID == CurUser.UserID))
                                && p.ParentID == null);

            foreach (var item in func)
            {
                nav.AppendLine
                (
                    "<li id='" + item.FunctionName + "'><a href='" + ResolveUrl(item.Url) + "'><span class='isw-archive'></span><span class='text'>" + item.DisplayText + "</span></a></li>"
                );
            }
            nav.AppendLine("</ul>");
            ltrNavigation.Text = nav.ToString();
        }

        protected void lbtnLogout_Click(object sender, EventArgs e)
        {
            try
            {
                CurUser = null;
                ReturnURL = Request.RawUrl;
                RedirectToPage(LoginURL);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"" + ex.Message + "\");", true);
            }
        }

        protected void lbtnChangePassword_Click(object sender, EventArgs e)
        {
            RedirectToPage(ChangePasswordURL);
        }
    }
}